import React, { Component, lazy, Suspense } from "react";

import { Link, Switch, Route } from "react-router-dom";

// import Home from "./pages/Home";
// import About from "./pages/About";

const Home = lazy(() => import("./pages/Home"));
const About = lazy(() => import("./pages/About"));

const Loading = () => {
  return <div>loading...</div>;
};

class App extends Component {
  render() {
    return (
      <>
        <h2>路由懒加载</h2>
        <ul>
          <li>
            <Link to="/home">首页</Link>
          </li>
          <li>
            <Link to="/about">关于页</Link>
          </li>
        </ul>

        <hr />

        {/* Suspense需要在路由每个路由组件的外面套住 */}
        <Switch>
          <Route path="/home">
            <Suspense fallback={<Loading />}>
              <Home />
            </Suspense>
          </Route>
          <Route path="/about">
            <Suspense fallback={<Loading />}>
              <About />
            </Suspense>
          </Route>
        </Switch>
      </>
    );
  }
}

export default App;
